package com.example.demo.common.util;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.demo.common.util.entity.ListSub;
import com.example.demo.common.util.entity.Result;
import com.example.demo.common.util.entity.ResultEnum;

import java.util.List;
import java.util.Objects;

public class ResultUtil {

    /**
     * 默认枚举类返回
     * @param resultEnum
     * @param <T>
     * @return
     */
    public static <T extends ResultEnum> Result info(T resultEnum){
        Result result = new Result();
        result.setCode(resultEnum.getCode());
        result.setMessage(resultEnum.getMsg());
        return result;
    }

    /**
     * 成功但不返回数据
     * @return
     */
    public static Result success(){
        Result result = new Result();
        result.setCode(ResultEnum.SUCCESS.getCode());
        result.setMessage(ResultEnum.SUCCESS.getMsg());
        return result;
    }

    /**
     * 成功但不返回数据
     * @param code
     * @param msg
     * @return
     */
    public static Result success(Integer code, String msg){
        Result result = new Result();
        result.setCode(code);
        result.setMessage(msg);
        return result;
    }

    /**
     * 成功且返回分页数据
     * @param iPage
     * @param <T>
     * @return
     */
    public static <T> Result<ListSub<T>> success(IPage<T> iPage){
        if(Objects.isNull(iPage)){
            return success();
        }
        ListSub<T> listSub = new ListSub<>();
        listSub.setTotal(iPage.getTotal());
        listSub.setList(iPage.getRecords());
        Result<ListSub<T>> result = new Result<>();
        result.setCode(ResultEnum.SUCCESS.getCode());
        result.setMessage(ResultEnum.SUCCESS.getMsg());
        result.setData(listSub);
        return result;
    }

    /**
     * 成功且返回列表数据
     * @param list
     * @param total
     * @param <T>
     * @return
     */
    public static <T> Result<ListSub<T>> success(List<T> list, Long total){
        ListSub<T> listSub = new ListSub();
        listSub.setList(list);
        listSub.setTotal(total);
        Result<ListSub<T>> result = new Result<>();
        result.setCode(ResultEnum.SUCCESS.getCode());
        result.setMessage(ResultEnum.SUCCESS.getMsg());
        result.setData(listSub);
        return result;
    }

    /**
     * 成功且返回任何数据
     * @param t
     * @param <T>
     * @return
     */
    public static <T> Result success(T t) {
        Result<T> result = new Result<>();
        result.setCode(ResultEnum.SUCCESS.getCode());
        result.setMessage(ResultEnum.SUCCESS.getMsg());
        result.setData(t);
        return result;
    }

    /**
     * 默认失败
     * @return
     */
    public static Result error(){
        Result result = new Result();
        result.setCode(ResultEnum.ERROR.getCode());
        result.setMessage(ResultEnum.ERROR.getMsg());
        return result;
    }

    /**
     * 失败且返回任何数据
     * @param t
     * @return
     */
    public static <T> Result error(T t){
        Result result = new Result();
        result.setCode(ResultEnum.ERROR.getCode());
        result.setMessage(ResultEnum.ERROR.getMsg());
        result.setData(t);
        return result;
    }

    /**
     * 默认返回异常信息
     * @param ex
     * @return
     */
    public static Result error(Exception ex){
        Result result = new Result();
        result.setCode(ResultEnum.UNKNOWN_ERROR.getCode());
        result.setMessage(ex.getMessage() + "\r\n堆栈信息：" + ex.getStackTrace());
        return result;
    }

    /**
     * 自定义失败
     * @param code
     * @param msg
     * @return
     */
    public static Result error(Integer code, String msg){
        Result result = new Result();
        result.setCode(code);
        result.setMessage(msg);
        return result;
    }
}
